<html>
<head><meta charset="utf-8"><title>Code Style process · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html">Code Style process</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207623752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207623752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207623752">(Aug 21 2020 at 11:29)</a>:</h4>
<p>What is the process for agreeing on "code style" for the compiler? Do we have/need this process at all?</p>
<p>A specific issue I am facing right now:</p>
<p>I need to decide between </p>
<div class="codehilite"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">foo</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>

<span class="k">mod</span> <span class="nn">bar</span><span class="p">;</span><span class="w"></span>
</code></pre></div>


<p>and </p>
<div class="codehilite"><pre><span></span><code><span class="k">mod</span> <span class="nn">bar</span><span class="p">;</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">foo</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
</code></pre></div>


<p>Looking at the compiler's source, both versions exist (<code>cosnt_eval/mod.rs</code> vs <code>parser/mod.rs</code>), so it's unclear to me how to proceed.</p>



<a name="207631643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207631643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207631643">(Aug 21 2020 at 13:08)</a>:</h4>
<p>For any question that also applies to arbitrary Rust code, you might bring it to the style team.</p>



<a name="207632090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207632090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207632090">(Aug 21 2020 at 13:12)</a>:</h4>
<p>all <code>use</code> before other stuff is also a fair rule to stick to I think. (but I'm not the style team)</p>



<a name="207632427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207632427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207632427">(Aug 21 2020 at 13:16)</a>:</h4>
<p>I personally prefer to put a mod statement before a use from the same mod.</p>



<a name="207632484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207632484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207632484">(Aug 21 2020 at 13:16)</a>:</h4>
<p>But for any other use, yeah, I prefer all use statements before all mod statements.</p>



<a name="207826978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207826978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207826978">(Aug 24 2020 at 10:15)</a>:</h4>
<p>FWIW, I personally feel moderately strongly that "all mod before any use" is a good rule</p>
<ol>
<li>it doesn't have same-mod exception (this is a big one)</li>
<li>set of submodules is the most important thing when you look at the module impl file, so it's good if it is on top.</li>
<li>in <code>2018</code>, mods are a kind of import anyway, as there's no need for <code>self::</code></li>
</ol>
<p>Putting <code>mod</code>s first does feel weird and counter-intuitive at the first glance, but, after I've tried this, I can not go back (unless I am overriden by project's style guide)</p>



<a name="207867192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207867192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207867192">(Aug 24 2020 at 16:54)</a>:</h4>
<p>most of my mod statements live inside macro invocations.</p>



<a name="207867698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207867698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207867698">(Aug 24 2020 at 16:59)</a>:</h4>
<p>so i basically always do:</p>
<ul>
<li>out of crate <code>use</code></li>
<li>private macros needed in sub-modules</li>
<li>sub-module declarations</li>
<li>possibly code in <a href="http://lib.rs">lib.rs</a> if needed</li>
</ul>
<p>and then all sub-modules are generally just <code>use super::*;</code> and then their code.</p>



<a name="207875251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207875251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207875251">(Aug 24 2020 at 18:02)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> I tend to use "all uses other than uses from mods declared here, then all mods, then all uses of mods".</p>



<a name="207875365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207875365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207875365">(Aug 24 2020 at 18:02)</a>:</h4>
<p>I already tend to do three blocks of <code>use</code> statements: std/core, third-party crates, this crate. mod statements go between the second and third of those, for me.</p>



<a name="207877192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207877192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207877192">(Aug 24 2020 at 18:16)</a>:</h4>
<p>Because someone gave the eyes react, here ya go:</p>
<div class="codehilite"><pre><span></span><code><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">submodule</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="p">(</span><span class="cp">$v</span>:<span class="nc">vis</span><span class="w"> </span><span class="cp">$name</span>:<span class="nc">ident</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">mod</span> <span class="cp">$name</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="cp">$v</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="cp">$name</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>
<span class="w">  </span><span class="p">(</span><span class="cp">$v</span>:<span class="nc">vis</span><span class="w"> </span><span class="cp">$name</span>:<span class="nc">ident</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($content</span>:<span class="nc">tt</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">mod</span> <span class="cp">$name</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($content</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="cp">$v</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="cp">$name</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>"you may not like it, but this is what peak performance really looks like."</p>



<a name="207877409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207877409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207877409">(Aug 24 2020 at 18:18)</a>:</h4>
<p>So you just want to split modules into separate files without logically partitioning the namespace?</p>



<a name="207877733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Code%20Style%20process/near/207877733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Code.20Style.20process.html#207877733">(Aug 24 2020 at 18:20)</a>:</h4>
<p>that is one reason, though a totally flat crate isn't always the best and I change the macro as appropriate.</p>
<p>the other reason is that it there's now only one #[cfg()] required instead of one on the <code>mod</code> and a separate one on it's associated <code>use</code> statement.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>